'use strict';

import HTTPUtil from './HTTPUtil'
import Global from './GlobalConst';
import Toast from '../Utils/toast';
export default class RuralAPIService {

    /**
     * 城市列表
     */
    static CityList(search) {
        var url = Global.HOST + '/Home/CityList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search", search);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                        resolve([]);
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 地区列表
     */
    static AreaList(id) {
        var url = Global.HOST + '/Home/getAreaList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                        reject(new Error(json.result));
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 镇列表
     */
    static RecommendedTown(id) {
        var url = Global.HOST + '/Home/RecommendedTown';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * U乡村
     */
    static HomeIndex() {
        var url = Global.HOST + '/Home/Index';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            HTTPUtil.post(url, null).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 攻略列表
     */
    static StrategyList(page) {
        var url = Global.HOST + '/Find/StrategyList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 活动列表
     */
    static ActivityList(page) {
        var url = Global.HOST + '/Find/ActivityList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 问答列表
     */
    static QuestinAnswerList(page) {
        var url = Global.HOST + '/Find/QuestinAnswerList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 游记列表
     */
    static TravelList(page) {
        var url = Global.HOST + '/Find/TravelList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 帮购列表
     */
    static HelpToByList(page) {
        var url = Global.HOST + '/Find/HelpToByList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 自驾约伴列表
     */
    static WithTogetherList(page) {
        var url = Global.HOST + '/Find/WithTogetherList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 首页搜索
     */
    static HomeSearch(search) {
        var url = Global.HOST + '/Home/Search';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search", search);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                        resolve([]);
                    } else {
                        Toast.show('网络错误');
                        reject(new Error(json.result));
                    }
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 镇详情
     */
    static TownDetails(id) {
        var url = Global.HOST + '/Home/TownDetails';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("id", id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 发现、搜索
     */
    static IndexList(search,page) {
        var url = Global.HOST + '/Find/IndexList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("search",search);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                        reject(new Error(json));
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 热门标签
     */
    static HotLabels() {
        var url = Global.HOST + '/Find/HotLabels';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            HTTPUtil.post(url, null).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
    /**
     * 热门搜索热门
     */
    static HotSearchLabels(type) {
        var url = Global.HOST + '/Find/HotLabels';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("type",type);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == 200) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
}